package com.lucenly.card.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lucenly.card.R;
import com.lucenly.card.adapter.WithdrawAdapter;
import com.lucenly.card.base.BaseActivity;
import com.lucenly.card.bean.WithdrawBean;
import com.lucenly.card.constants.UrlDefend;
import com.lucenly.card.utils.GsonUtil;
import com.vise.xsnow.http.ViseHttp;
import com.vise.xsnow.http.callback.ACallback;
import com.vise.xsnow.utls.SharedPreferencesUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import ezy.ui.layout.LoadingLayout;

public class WithdrawListActivity extends BaseActivity {
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.ll_back)
    LinearLayout ll_back;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.view)
    LoadingLayout view;
    WithdrawAdapter adapter;
    List<WithdrawBean> list = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_withdraw_list);
    }

    @Override
    protected void initView() {
        tv_title.setText("提现记录");
        view.showLoading();
    }

    @Override
    protected void bindEvent() {
        ll_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    protected void initData() {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new WithdrawAdapter();
        adapter.replaceData(list);
        adapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        recyclerView.setAdapter(adapter);
        getData();
    }

    private void getData() {
        ViseHttp.GET(UrlDefend.TIXIAN_JILU)
                .addHeader("Token", SharedPreferencesUtil.getInstance().getString("Token"))
                .request(new ACallback<String>() {
                    @Override
                    public void onSuccess(String data) {
                        try {
                            JSONObject jsonObject = new JSONObject(data);
                            JSONArray lists = jsonObject.getJSONArray("list");
                            ArrayList<WithdrawBean> withdrawBeans = GsonUtil.fromJsonList(lists.toString(), WithdrawBean.class);
                            if(withdrawBeans==null||withdrawBeans.size()==0){
                                view.showEmpty();
                            }else{
                                adapter.replaceData(withdrawBeans);
                                view.showContent();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                            view.showError();
                        }
                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {
                        view.showEmpty();
                    }
                });
    }

    @Override
    protected void processClick(View view) {

    }
}
